package org.myorg.v3;

//import java.io.Serializable;
//import java.util.Random;

public class GA3 {
	static int best_p;
	static double best_v;
	static FPeople3 best_people;


	public static String ga(int geneLength,
			int peopleNum, double p_cross,
			double p_mut, long i_num,Fc3 fc) {

		String res = "";
		double g_b = Double.NEGATIVE_INFINITY;

		long start_time = System.currentTimeMillis();
		FPeople3 my_p = new FPeople3(peopleNum,geneLength);
		FEvolution3 my_e = new FEvolution3(0.1, p_cross, p_mut);
		fc.caculate(my_p.persons);

		for (int i=0; i < i_num; i++) {

			/*if current best value are letter than global best value,
			 * replace global best value with current value
			 */
			//double c_b = my_p.getBest().fitness;
/*			if (c_b > g_b) {
				g_b = c_b;*/
				int is_r = fc.is_r(my_p.getBest());
				if (1 == is_r ||  0 == is_r ) {
					res = res + "\t" + (System.currentTimeMillis() - start_time) 
					+"\t" + i + "\t" + fc.org_value(my_p.getBest());
					if (0 == is_r ) {
						break;
					}
				}
/*			}*/
			my_e.select(my_p);
			my_e.crossOver(my_p);
			my_e.mutation(my_p);
			fc.caculate(my_p.persons);
		}

		return res;
	}
	
	public static void main(String[] arg) {
		System.out.println("enter main");
		double[] data = {10E6, 10E5}; 
		//double[] data = {23, 18, 4, 3, 2, 1}; 
		Fc3 fc = new FF31(data);
		//double[] data = { 0.9, 0.99, 0.999, 0.9999, 0.99999};
		//double[] data = {0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.999};
		//double[] data = {1.0E-12, 5.0E-11, 4.0E-11, 3.0E-11 , 2.0E-11};
		//double[] data = {1.0E12, 5.0E11, 4.0E11, 3.0E11 , 1.0E10, 1.0E09, 1.0E08, 1.0E07};
		String tmp = ga(2, 100, 0.5, 0.5, 10000000, fc);
		System.out.println(tmp);
		
		System.out.println("leave main");
	}

}









